getwd()

install.packages("dplyr")
install.packages("dwplot")
install.packages("magrittr")
install.packages("dotwhisker")
packageVersion("dotwhisker")
install.packages("arsenal")  
install.packages("readxl")
install.packages("stargazer")  

library(broom)  # 用于整理模型结果
library(ggplot2)
library(dotwhisker)
library("readxl")
library(writexl)
library(texreg)
library(foreign)
library(magrittr) # needs to be run every time you start R and want to use %>%
library(dplyr)    # alternatively, this also loads %>%
library(dwplot)    # alternatively, this also loads %>%ff
library(stargazer)
library(sjPlot)
library(readr)
library(tableby)
library(readxl)         
library(arsenal)         

###### Import Data###### 
Air <- read_xlsx('Air.xlsx')
WaterResource <- read_xlsx('WaterResource.xlsx')
Green <- read_xlsx('Green.xlsx')
WaterSaving <- read_xlsx('WaterSaving.xlsx')
Noise <- read_xlsx('Noise.xlsx')
Wetland <- read_xlsx('Wetland.xlsx')
WaterPollution <- read_xlsx('WaterPollution.xlsx')

CombinedData <- rbind( WaterResource, Air, WaterSaving ,  Green,  Noise,  Wetland, WaterPollution)
CombinedData$Policy.f <- factor(CombinedData$Policy)

###### Table1 ###### 
fit1 <- lm(similarity ~ horizon_learner +  Distance + differ_year +  innovator_learner_leader + SimilarSpecialization + city_learner + Self_learer.f + ABSforeigInvestmentBillion_learner_leader + Population_learner_leader + BirthRate_learner_leader + SecondIndustr_learner_leader+ Learner_ID.f + Leader_ID.f + Year_learner.f , data = Air  , na.action = na.omit)
fit2 <- lm(similarity ~ horizon_learner +  Distance + differ_year +  innovator_learner_leader + SimilarSpecialization + city_learner + Self_learer.f + ABSforeigInvestmentBillion_learner_leader + Population_learner_leader + BirthRate_learner_leader + SecondIndustr_learner_leader+ Learner_ID.f + Leader_ID.f + Year_learner.f , data = Green, na.action = na.omit)
fit3 <- lm(similarity ~ horizon_learner +  Distance + differ_year +  innovator_learner_leader + SimilarSpecialization + city_learner + Self_learer.f + ABSforeigInvestmentBillion_learner_leader + Population_learner_leader + BirthRate_learner_leader + SecondIndustr_learner_leader+ Learner_ID.f + Leader_ID.f + Year_learner.f , data = Noise, na.action = na.omit)
fit4 <- lm(similarity ~ horizon_learner +  Distance + differ_year +  innovator_learner_leader + SimilarSpecialization + city_learner + Self_learer.f + ABSforeigInvestmentBillion_learner_leader + Population_learner_leader + BirthRate_learner_leader + SecondIndustr_learner_leader+ Learner_ID.f + Leader_ID.f + Year_learner.f, data =  WaterResource , na.action = na.omit)
fit5 <- lm(similarity ~ horizon_learner +  Distance + differ_year +  innovator_learner_leader + SimilarSpecialization + city_learner + Self_learer.f + ABSforeigInvestmentBillion_learner_leader + Population_learner_leader + BirthRate_learner_leader + SecondIndustr_learner_leader+ Learner_ID.f + Leader_ID.f + Year_learner.f , data = WaterSaving , na.action = na.omit)
fit6 <- lm(similarity ~ horizon_learner +  Distance + differ_year +  innovator_learner_leader + SimilarSpecialization + city_learner + Self_learer.f + ABSforeigInvestmentBillion_learner_leader + Population_learner_leader + BirthRate_learner_leader + SecondIndustr_learner_leader+ Learner_ID.f + Leader_ID.f + Year_learner.f, data = Wetland , na.action = na.omit)
fit7 <- lm(similarity ~ horizon_learner +  Distance + differ_year +  innovator_learner_leader + SimilarSpecialization + city_learner + Self_learer.f + ABSforeigInvestmentBillion_learner_leader + Population_learner_leader + BirthRate_learner_leader + SecondIndustr_learner_leader+ Learner_ID.f + Leader_ID.f + Year_learner.f, data = WaterPollution , na.action = na.omit)
fit8 <- lm(similarity ~ horizon_learner +  Distance + differ_year +  innovator_learner_leader + SimilarSpecialization + city_learner + Self_learer.f + ABSforeigInvestmentBillion_learner_leader + Population_learner_leader + BirthRate_learner_leader + SecondIndustr_learner_leader + Policy.f+ Learner_ID.f + Leader_ID.f + Year_learner.f, data = CombinedData , na.action = na.omit)

### export table 1
stargazer(fit8, fit1, fit2,fit3,fit4,fit5,fit6,fit7, type = "html",  #we use html output to match our planned R Markdown output
          title = "Linear Regression of Policy Learning - Table1",df = FALSE, notes = "Robust Standard Error in the Parentheses",
          out = "Table1 r&r.htm", omit = c("Year_learner", "Learner_ID", "Leader_ID", "Policy"),
          covariate.labels = c("Horizontal Learning", "Spatial Distance", "Temporal Distance", "Initial Learning",
                               "Similar Judiciary System", "City Learner", "Self-Learning", "Foreign Investment Difference",
                               "Population Difference","Birthrate Difference","Secondary Industry Difference"), #,"Foreign Investment" ,"pm25"
          column.labels = c("Overall",  "Air Pollution", "Green Space", "Noise Pollution", "Water Resource", "Water Saving","Wetland","Water Pollution"),
          add.lines = list(c("Learner Location Dummies","Yes",  "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes"),
                           c("Learner Year Dummies", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes"),
                           c("Source Location Dummies","Yes",  "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes"),
                           c("Policy Fixed effects","Yes",  "N/A", "N/A", "N/A", "N/A", "N/A", "N/A", "N/A")
          ))



###### Table2 ###### 
library(tidyverse)   # DATA CLEANING FUNCTIONS
library(lme4)        # MULTILEVEL MODELS
library(texreg)      # DISPLAY MODELS

fit1 <- lmer(similarity ~ horizon_learner +  Distance + differ_year +  innovator_learner_leader + SimilarSpecialization + city_learner + Self_learer.f + ABSforeigInvestmentBillion_learner_leader + Population_learner_leader + BirthRate_learner_leader + SecondIndustr_learner_leader + Year_learner.f +  (1|Learner_ID.f) + (1|Leader_ID.f) + (1|Dyadic_ID)  , data = Air  , na.action = na.omit)
fit2 <- lmer(similarity ~ horizon_learner +  Distance + differ_year +  innovator_learner_leader + SimilarSpecialization + city_learner + Self_learer.f + ABSforeigInvestmentBillion_learner_leader + Population_learner_leader + BirthRate_learner_leader + SecondIndustr_learner_leader + Year_learner.f +  (1|Learner_ID.f) + (1|Leader_ID.f) + (1|Dyadic_ID)  , data = Green , na.action = na.omit)
fit3 <- lmer(similarity ~ horizon_learner +  Distance + differ_year +  innovator_learner_leader + SimilarSpecialization + city_learner + Self_learer.f + ABSforeigInvestmentBillion_learner_leader + Population_learner_leader + BirthRate_learner_leader + SecondIndustr_learner_leader + Year_learner.f +  (1|Learner_ID.f) + (1|Leader_ID.f) + (1|Dyadic_ID)  , data = Noise , na.action = na.omit)
fit4 <- lmer(similarity ~ horizon_learner +  Distance + differ_year +  innovator_learner_leader + SimilarSpecialization + city_learner + Self_learer.f + ABSforeigInvestmentBillion_learner_leader + Population_learner_leader + BirthRate_learner_leader + SecondIndustr_learner_leader + Year_learner.f +  (1|Learner_ID.f) + (1|Leader_ID.f) + (1|Dyadic_ID) , data =  WaterResource, na.action = na.omit)
fit5 <- lmer(similarity ~ horizon_learner +  Distance + differ_year +  innovator_learner_leader + SimilarSpecialization + city_learner + Self_learer.f + ABSforeigInvestmentBillion_learner_leader + Population_learner_leader + BirthRate_learner_leader + SecondIndustr_learner_leader + Year_learner.f +  (1|Learner_ID.f) + (1|Leader_ID.f) + (1|Dyadic_ID)  , data = WaterSaving , na.action = na.omit)
fit6 <- lmer(similarity ~ horizon_learner +  Distance + differ_year +  innovator_learner_leader + SimilarSpecialization + city_learner + Self_learer.f + ABSforeigInvestmentBillion_learner_leader + Population_learner_leader + BirthRate_learner_leader + SecondIndustr_learner_leader + Year_learner.f +  (1|Learner_ID.f) + (1|Leader_ID.f) + (1|Dyadic_ID) , data =  Wetland , na.action = na.omit)
fit7 <- lmer(similarity ~ horizon_learner +  Distance + differ_year +  innovator_learner_leader + SimilarSpecialization + city_learner + Self_learer.f + ABSforeigInvestmentBillion_learner_leader + Population_learner_leader + BirthRate_learner_leader + SecondIndustr_learner_leader + Year_learner.f +  (1|Learner_ID.f) + (1|Leader_ID.f) + (1|Dyadic_ID) , data = WaterPollution , na.action = na.omit)
fit8 <- lmer(similarity ~ horizon_learner +  Distance + differ_year +  innovator_learner_leader + SimilarSpecialization + city_learner + Self_learer.f + ABSforeigInvestmentBillion_learner_leader + Population_learner_leader + BirthRate_learner_leader + SecondIndustr_learner_leader + Policy.f + Year_learner.f +  (1|Learner_ID.f) + (1|Leader_ID.f) + (1|Dyadic_ID) , data = CombinedData , na.action = na.omit)

###### Table2 ###### 
multilevelmodel_list <- list(fit8,fit1,fit2, fit3,fit4,fit5,fit6,fit7)
write_rds(multilevelmodel_list, path = "model_maintext_list.rds")

## 1B. LOAD DATA    -----------------------------
## models fit in "2-analysis/SC-Tables-Step1- Fit Models.R"

# LIST OF MODELS FOR MAIN TEXT
model_maintext_list <- read_rds("model_maintext_list.rds")

# 2. MAKE TABLE (MAIN TEXT) -----------------------

mod_names <- paste0("(", seq_along(model_maintext_list), ")")

the_cap <- "Multilevel linear regressions of text similarity (with standard errors in parentheses)"

var_map <- list(
  horizon_learner = "Horizontal Learning",
  Distance  = "Spatial Distance",
  differ_year    = "Temporal Distance",
  innovator_learner_leader   = "Initial Learning",
  SimilarSpecialization1 = "Similar Judiciary System",
  city_learner   = "City Learner",
  Self_learer.f1  = "Self-Learning",
  ABSforeigInvestmentBillion_learner_leader       = "Foreign Investment",
  Population_learner_leader      = "Population Difference",
  BirthRate_learner_leader       = "Birthrate Difference",
  SecondIndustr_learner_leader   = "Secondary Industry Difference",
  `(Intercept)`      = "Intercept"
)


gof_names <- c(
  `AIC`= "AIC",
  `Num. obs:`= "Observations",
  #  `Num. obs.`= "N - Dyads",
  `Num. groups: Dyadic_ID.f`= "N - Dyad ID",
  `Num. groups: Learner_ID.f`= "N -Learner Location",
  `Num. groups: Leader_ID.f`= "N - Source Location",
  `Var: Dyadic_ID (Intercept)`= "&sigma;<sup>2</sup><sub>Dyad ID</sub>",
  `Var: Learner_ID.f (Intercept)`= "&sigma;<sup>2</sup><sub>Learner Location</sub>",
  `Var: Leader_ID.f (Intercept)`= "&sigma;<sup>2</sup><sub>Source Location</sub>",
  `Var: Residual`= "&sigma;<sup>2</sup><sub>Residual</sub>"
)

htmlreg(model_maintext_list,
        caption = " ",
        custom.model.names = mod_names,
        caption.above = TRUE,
        #        stars = 0.05,
        #        bold  = 0.05,
        #       groups = group_list,
        custom.coef.map = var_map,
        custom.gof.names = gof_names,
        single.row = FALSE,
        #       custom.note = "* p < 0.05 (two-tailed)",
        file = "Table2.doc",
        doctype = TRUE,
        html.tag = TRUE,
        head.tag = TRUE,
        body.tag = TRUE,
        digits = 3,
        include.loglik = FALSE,
        include.bic = FALSE,
        #        reorder.gof = c(2:7, 1)
)


###### Figure 2 ###### 
fit1 <- lm(similarity ~ horizon_learner +  Distance + differ_year +  innovator_learner_leader + SimilarSpecialization + city_learner + Self_learer.f + ABSforeigInvestmentBillion_learner_leader + Population_learner_leader + BirthRate_learner_leader + SecondIndustr_learner_leader+ Learner_ID.f + Leader_ID.f + Year_learner.f , data = Air  , na.action = na.omit)
fit2 <- lm(similarity ~ horizon_learner +  Distance + differ_year +  innovator_learner_leader + SimilarSpecialization + city_learner + Self_learer.f + ABSforeigInvestmentBillion_learner_leader + Population_learner_leader + BirthRate_learner_leader + SecondIndustr_learner_leader+ Learner_ID.f + Leader_ID.f + Year_learner.f , data = Green , na.action = na.omit)
fit3 <- lm(similarity ~ horizon_learner +  Distance + differ_year +  innovator_learner_leader + SimilarSpecialization + city_learner + Self_learer.f + ABSforeigInvestmentBillion_learner_leader + Population_learner_leader + BirthRate_learner_leader + SecondIndustr_learner_leader+ Learner_ID.f + Leader_ID.f + Year_learner.f , data = Noise , na.action = na.omit)
fit4 <- lm(similarity ~ horizon_learner +  Distance + differ_year +  innovator_learner_leader + SimilarSpecialization + city_learner + Self_learer.f + ABSforeigInvestmentBillion_learner_leader + Population_learner_leader + BirthRate_learner_leader + SecondIndustr_learner_leader+ Learner_ID.f + Leader_ID.f + Year_learner.f, data = WaterResource , na.action = na.omit)
fit5 <- lm(similarity ~ horizon_learner +  Distance + differ_year +  innovator_learner_leader + SimilarSpecialization + city_learner + Self_learer.f + ABSforeigInvestmentBillion_learner_leader + Population_learner_leader + BirthRate_learner_leader + SecondIndustr_learner_leader+ Learner_ID.f + Leader_ID.f + Year_learner.f , data = WaterSaving , na.action = na.omit)
fit6 <- lm(similarity ~ horizon_learner +  Distance + differ_year +  innovator_learner_leader + SimilarSpecialization + city_learner + Self_learer.f + ABSforeigInvestmentBillion_learner_leader + Population_learner_leader + BirthRate_learner_leader + SecondIndustr_learner_leader+ Learner_ID.f + Leader_ID.f + Year_learner.f, data = Wetland , na.action = na.omit)
fit7 <- lm(similarity ~ horizon_learner +  Distance + differ_year +  innovator_learner_leader + SimilarSpecialization + city_learner + Self_learer.f + ABSforeigInvestmentBillion_learner_leader + Population_learner_leader + BirthRate_learner_leader + SecondIndustr_learner_leader+ Learner_ID.f + Leader_ID.f + Year_learner.f, data = WaterPollution , na.action = na.omit)
fit8 <- lm(similarity ~ horizon_learner +  Distance + differ_year +  innovator_learner_leader + SimilarSpecialization + city_learner + Self_learer.f + ABSforeigInvestmentBillion_learner_leader + Population_learner_leader + BirthRate_learner_leader + SecondIndustr_learner_leader + Policy.f+ Learner_ID.f + Leader_ID.f + Year_learner.f, data = CombinedData , na.action = na.omit)


m1_fit1_1 <- broom::tidy(fit1) %>% mutate(model = "Air Pollution") %>% 
  filter(!grepl("Learner_ID.*|Leader_ID.*|Year_learner.*|SimilarSpecialization|city_learner|Self_learer.*|ABSforeigInvestmentBillion_learner_leader|Population_learner_leader|BirthRate_learner_leader|SecondIndustr_learner_leader", term)) 
m1_fit2_1 <- broom::tidy(fit2) %>% mutate(model = "Greening") %>% 
  filter(!grepl("Learner_ID.*|Leader_ID.*|Year_learner.*|SimilarSpecialization|city_learner|Self_learer.*|ABSforeigInvestmentBillion_learner_leader|Population_learner_leader|BirthRate_learner_leader|SecondIndustr_learner_leader", term)) 
m1_fit3_1 <- broom::tidy(fit3) %>% mutate(model = "Noise Pollution") %>% 
  filter(!grepl("Learner_ID.*|Leader_ID.*|Year_learner.*|SimilarSpecialization|city_learner|Self_learer.*|ABSforeigInvestmentBillion_learner_leader|Population_learner_leader|BirthRate_learner_leader|SecondIndustr_learner_leader", term)) 
m1_fit4_1 <- broom::tidy(fit4) %>% mutate(model = " Water Resource") %>% 
  filter(!grepl("Learner_ID.*|Leader_ID.*|Year_learner.*|SimilarSpecialization|city_learner|Self_learer.*|ABSforeigInvestmentBillion_learner_leader|Population_learner_leader|BirthRate_learner_leader|SecondIndustr_learner_leader", term)) 
m1_fit5_1 <- broom::tidy(fit5) %>% mutate(model = "Water Saving") %>% 
  filter(!grepl("Learner_ID.*|Leader_ID.*|Year_learner.*|SimilarSpecialization|city_learner|Self_learer.*|ABSforeigInvestmentBillion_learner_leader|Population_learner_leader|BirthRate_learner_leader|SecondIndustr_learner_leader", term)) 
m1_fit6_1 <- broom::tidy(fit6) %>% mutate(model = "Wetland") %>% 
  filter(!grepl("Learner_ID.*|Leader_ID.*|Year_learner.*|SimilarSpecialization|city_learner|Self_learer.*|ABSforeigInvestmentBillion_learner_leader|Population_learner_leader|BirthRate_learner_leader|SecondIndustr_learner_leader", term)) 
m1_fit7_1 <- broom::tidy(fit7) %>% mutate(model = "Water Pollution") %>% 
  filter(!grepl("Learner_ID.*|Leader_ID.*|Year_learner.*|SimilarSpecialization|city_learner|Self_learer.*|ABSforeigInvestmentBillion_learner_leader|Population_learner_leader|BirthRate_learner_leader|SecondIndustr_learner_leader", term)) 

# replacing the first column with label name
m1_fit1_1$term <- c("(Intercept)","H1: Horizontal Learning", "H2: Spatial Distance", "H3: Temporal Distance",  "H4: Initial Learning")
m1_fit2_1$term <- c("(Intercept)","H1: Horizontal Learning", "H2: Spatial Distance", "H3: Temporal Distance",  "H4: Initial Learning")
m1_fit3_1$term <- c("(Intercept)","H1: Horizontal Learning", "H2: Spatial Distance", "H3: Temporal Distance",  "H4: Initial Learning")
m1_fit4_1$term <- c("(Intercept)","H1: Horizontal Learning", "H2: Spatial Distance", "H3: Temporal Distance",  "H4: Initial Learning")
m1_fit5_1$term <- c("(Intercept)","H1: Horizontal Learning", "H2: Spatial Distance", "H3: Temporal Distance",  "H4: Initial Learning")
m1_fit6_1$term <- c("(Intercept)","H1: Horizontal Learning", "H2: Spatial Distance", "H3: Temporal Distance",  "H4: Initial Learning")
m1_fit7_1$term <- c("(Intercept)","H1: Horizontal Learning", "H2: Spatial Distance", "H3: Temporal Distance",  "H4: Initial Learning")

Second4_models <- rbind( m1_fit1_1,  m1_fit2_1 , m1_fit3_1 , m1_fit4_1, m1_fit5_1, m1_fit6_1, m1_fit7_1)


dwplot(Second4_models) + #, dodge_size = 0.2,vline = geom_vline(xintercept = 0, colour = "grey50", linetype = 2)
  theme_bw(base_size = 16) +
  theme(legend.justification=c(.02, .993), legend.position=c(.75, .85),
        legend.title = element_blank(), legend.background =element_rect(color="gray90"))+
  scale_color_brewer(palette="Set1") +
  xlab("Text Similarity")+ 
  geom_vline(xintercept = 0, colour = "grey50", linetype = 2)


################ ################ ################################################ ################ ################################ 
################ Table A2. Descriptive Statistics ################################ 
CombinedData <- rbind( WaterResource, Air, WaterSaving, Green, Noise, Wetland, WaterPollution)
CombinedData_subset <- dplyr::select(CombinedData, similarity, horizon_learner.f, Distance, differ_year,innovator_learner_leader.f , SimilarSpecialization, 
                                     city_learner.f, Self_learer.f, ABSforeigInvestmentBillion_learner_leader, 
                                     Population_learner_leader, BirthRate_learner_leader, SecondIndustr_learner_leader
                                     , Policy) # , YearEnd_Population, BirthRate, SecondIndustrEmploy,


labels(CombinedData_subset)  <- c(similarity = 'Similarity', city_learner.f = "City Learner", horizon_learner.f = "Horizontal Learning"
                                  , differ_year = "Temporal Distance", Distance = "Spatial Distance", SimilarSpecialization = "Similar Judiciary System"
                                  , innovator_learner_leader.f ="Initial Learning", Self_learer.f = "Self-Learning"
                                  , ABSforeigInvestmentBillion_learner_leader = "Foreign Investment Difference"
                                  , Population_learner_leader = "Population Difference",  BirthRate_learner_leader = "Birthrate Difference"
                                  , SecondIndustr_learner_leader = "Secondary Industry Difference") # , SecondIndustrEmploy = "Secondary Industry", BirthRate ="Birth Rate", YearEnd_Population ="Population"

table_one <- tableby(Policy ~ ., data = CombinedData_subset , digits = 2) 
# summary(table_one, title = "Descriptive Statistics")
write2html(table_one, "Descriptive Statistics.html")  ##export to html 


################ ################ ################################################ ################ ################################ 
################ Table A3. Correlation Tables  ################################ 
CombinedData <- rbind( WaterResource, Air, WaterSaving ,  Green,  Noise,  Wetland, WaterPollution)
CombinedData$Policy.f <- factor(CombinedData$Policy)
CombinedData_subset2 <- dplyr::select( CombinedData, similarity, horizon_learner, Distance, differ_year, innovator_learner_leader, SimilarSpecialization, 
                                       city_learner, Self_learer, ABSforeigInvestmentBillion_learner_leader, 
                                       Population_learner_leader, BirthRate_learner_leader, SecondIndustr_learner_leader,
                                       Policy) # , YearEnd_Pop

CombinedData_subset2$SimilarSpecialization<-as.numeric(as.character(CombinedData_subset2$SimilarSpecialization))

WaterResou_subset <- CombinedData_subset2[CombinedData_subset2$Policy == "Water Resource Protection",]
Air_subset <- CombinedData_subset2[CombinedData_subset2$Policy == "Air Pollution",]
WaterSaving_subset <- CombinedData_subset2[CombinedData_subset2$Policy == "Water Saving",] 
Green_subset <- CombinedData_subset2[CombinedData_subset2$Policy == "Green Space",] 
Noise_subset <- CombinedData_subset2[CombinedData_subset2$Policy == "Noise Pollution",] 
Wetland_subset <- CombinedData_subset2[CombinedData_subset2$Policy == "Wetland Protection",] 
WaterPollution_subset <- CombinedData_subset2[CombinedData_subset2$Policy == "Water Pollution",] 


library(apaTables)
library(tidyr)
library(forcats)

# show.conf.interval :  This argument is deprecated and will be removed from later versions.
apa.cor.table(CombinedData_subset2, "Overall Correlation.doc", show.conf.interval=TRUE, landscape = TRUE)
apa.cor.table(WaterResou_subset, "Water Resource Correlation.doc", show.conf.interval=TRUE, landscape = TRUE)
apa.cor.table(Air_subset, "Air Correlation.doc", show.conf.interval=FALSE, landscape = TRUE)
apa.cor.table(WaterSaving_subset, "Water Saving  Correlation.doc", show.conf.interval=FALSE, landscape = TRUE)
apa.cor.table(Green_subset, "Green Space Correlation.doc", show.conf.interval=FALSE, landscape = TRUE)
apa.cor.table(Noise_subset, "Noise Correlation.doc", show.conf.interval=FALSE, landscape = TRUE)
apa.cor.table(Wetland_subset, "Wetland Correlation.doc", show.conf.interval=FALSE, landscape = TRUE)
apa.cor.table(WaterPollution_subset, "Water Pollution.doc", show.conf.interval=FALSE, landscape = TRUE)



## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## 
################ Figure A3. Histogram of Similarity Score  ################################ 
hist(WaterResource$similarity)
h = hist(WaterResource$similarity, breaks=25) #, border=F, col="gray", xlab="Histogram of Text Similarity", main="Water Resource Protection Policy")
h$density = h$counts/sum(h$counts)
jpeg("WaterResourcebar.jpg") 
plot(h, freq=FALSE, border=F, col="gray", xlab="Histogram of Text Similarity", main="Water Resource Protection Policy")
dev.off()
sum(h$density)

h = hist(Air$similarity, breaks=25) #, border=F, col="gray", xlab="Histogram of Text Similarity", main="Water Resource Protection Policy")
h$density = h$counts/sum(h$counts)
jpeg("Airbar.jpg") 
plot(h, freq=FALSE, border=F, col="gray", xlab="Histogram of Text Similarity", main="Air Pollution Policy")
dev.off()
sum(h$density)

h = hist(Green$similarity, breaks=25) #, border=F, col="gray", xlab="Histogram of Text Similarity", main="Green Space Policy")
h$density = h$counts/sum(h$counts)
jpeg("Greenbar.jpg") 
plot(h, freq=FALSE, border=F, col="gray", xlab="Histogram of Text Similarity", main="Green Space Policy")
dev.off()
sum(h$density)

h = hist(WaterSaving$similarity, breaks=25) #, border=F, col="gray", xlab="Histogram of Text Similarity", main="Water Saving Policy")
h$density = h$counts/sum(h$counts)
jpeg("WaterSavingbar.jpg") 
plot(h, freq=FALSE, border=F, col="gray", xlab="Histogram of Text Similarity", main="Water Saving Policy")
sum(h$density)


h = hist(Noise$similarity, breaks=25) #, border=F, col="gray", xlab="Histogram of Text Similarity", main="Noise Pollution Policy")
h$density = h$counts/sum(h$counts)
jpeg("Noisebar.jpg") 
plot(h, freq=FALSE, border=F, col="gray", xlab="Histogram of Text Similarity", main="Noise Pollution Policy")
dev.off()
sum(h$density)

h = hist(Wetland$similarity, breaks=25) #, border=F, col="gray", xlab="Histogram of Text Similarity", main="Wetland Protection Policy")
h$density = h$counts/sum(h$counts)
jpeg("Wetlandbar.jpg") 
plot(h, freq=FALSE, border=F, col="gray", xlab="Histogram of Text Similarity", main="Wetland Protection Policy")
dev.off()
sum(h$density)

h = hist(WaterPollution$similarity, breaks=25) #, border=F, col="gray", xlab="Histogram of Text Similarity", main="Wetland Protection Policy")
h$density = h$counts/sum(h$counts)
jpeg("WaterPollutionbar.jpg") 
plot(h, freq=FALSE, border=F, col="gray", xlab="Histogram of Text Similarity", main="Water Pollution Policy")
dev.off()
sum(h$density)

h = hist(CombinedData$similarity, breaks=25) #, border=F, col="gray", xlab="Histogram of Text Similarity", main="Noise Pollution Policy")
h$density = h$counts/sum(h$counts)
jpeg("CombinedDatabar.jpg") 
plot(h, freq=FALSE, border=F, col="gray", xlab="Histogram of Text Similarity", main="Overall")
dev.off()
sum(h$density)
## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## 



## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## 
### export table A4-1
fit1 <- lm(similarity ~ horizon_learner + Learner_ID.f + Leader_ID.f + Year_learner.f , data = Air  , na.action = na.omit)
fit2 <- lm(similarity ~ horizon_learner + Learner_ID.f + Leader_ID.f + Year_learner.f , data = Green, na.action = na.omit)
fit3 <- lm(similarity ~ horizon_learner + Learner_ID.f + Leader_ID.f + Year_learner.f , data = Noise, na.action = na.omit)
fit4 <- lm(similarity ~ horizon_learner + Learner_ID.f + Leader_ID.f + Year_learner.f, data =  WaterResource , na.action = na.omit)
fit5 <- lm(similarity ~ horizon_learner + Learner_ID.f + Leader_ID.f + Year_learner.f , data = WaterSaving , na.action = na.omit)
fit6 <- lm(similarity ~ horizon_learner + Learner_ID.f + Leader_ID.f + Year_learner.f, data = Wetland , na.action = na.omit)
fit7 <- lm(similarity ~ horizon_learner + Learner_ID.f + Leader_ID.f + Year_learner.f, data = WaterPollution , na.action = na.omit)
fit8 <- lm(similarity ~ horizon_learner + Leader_ID.f + Year_learner.f, data = CombinedData , na.action = na.omit)

### export TableA4-1
stargazer(fit8, fit1, fit2,fit3,fit4,fit5,fit6,fit7, type = "html",  #we use html output to match our planned R Markdown output
          title = "Linear Regression of Policy Learning - Table1",df = FALSE, notes = "Robust Standard Error in the Parentheses",
          out = "appendix TableA4-1.htm", omit = c("Year_learner", "Learner_ID", "Leader_ID", "Policy"),
          covariate.labels = c("Horizontal Learning"), #,"Foreign Investment" ,"pm25"
          column.labels = c("Overall",  "Air Pollution", "Green Space", "Noise Pollution", "Water Resource", "Water Saving","Wetland","Water Pollution"),
          add.lines = list(c("Learner Location Dummies","Yes",  "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes"),
                           c("Learner Year Dummies", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes"),
                           c("Source Location Dummies","Yes",  "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes"),
                           c("Policy Fixed effects","Yes",  "N/A", "N/A", "N/A", "N/A", "N/A", "N/A", "N/A")
          ))

### export table A4-2
fit1 <- lm(similarity ~ Distance + Learner_ID.f + Leader_ID.f + Year_learner.f , data = Air  , na.action = na.omit)
fit2 <- lm(similarity ~ Distance + Learner_ID.f + Leader_ID.f + Year_learner.f , data = Green, na.action = na.omit)
fit3 <- lm(similarity ~ Distance + Learner_ID.f + Leader_ID.f + Year_learner.f , data = Noise, na.action = na.omit)
fit4 <- lm(similarity ~ Distance + Learner_ID.f + Leader_ID.f + Year_learner.f, data =  WaterResource , na.action = na.omit)
fit5 <- lm(similarity ~ Distance + Learner_ID.f + Leader_ID.f + Year_learner.f , data = WaterSaving , na.action = na.omit)
fit6 <- lm(similarity ~ Distance + Learner_ID.f + Leader_ID.f + Year_learner.f, data = Wetland , na.action = na.omit)
fit7 <- lm(similarity ~ Distance + Learner_ID.f + Leader_ID.f + Year_learner.f, data = WaterPollution , na.action = na.omit)
fit8 <- lm(similarity ~ Distance + Leader_ID.f + Year_learner.f, data = CombinedData , na.action = na.omit)

### export TableA4-2
stargazer(fit8, fit1, fit2,fit3,fit4,fit5,fit6,fit7, type = "html",  #we use html output to match our planned R Markdown output
          title = "Linear Regression of Policy Learning - Table1",df = FALSE, notes = "Robust Standard Error in the Parentheses",
          out = "appendix TableA4-2.htm", omit = c("Year_learner", "Learner_ID", "Leader_ID", "Policy"),
          covariate.labels = c("Spatial Distance"), #"Foreign Investment" ,"pm25"
          column.labels = c("Overall",  "Air Pollution", "Green Space", "Noise Pollution", "Water Resource", "Water Saving","Wetland","Water Pollution"),
          add.lines = list(c("Learner Location Dummies","Yes",  "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes"),
                           c("Learner Year Dummies", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes"),
                           c("Source Location Dummies","Yes",  "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes"),
                           c("Policy Fixed effects","Yes",  "N/A", "N/A", "N/A", "N/A", "N/A", "N/A", "N/A")
          ))

### export TableA4-3
fit1 <- lm(similarity ~ differ_year + Learner_ID.f + Leader_ID.f + Year_learner.f , data = Air  , na.action = na.omit)
fit2 <- lm(similarity ~ differ_year + Learner_ID.f + Leader_ID.f + Year_learner.f , data = Green, na.action = na.omit)
fit3 <- lm(similarity ~ differ_year + Learner_ID.f + Leader_ID.f + Year_learner.f , data = Noise, na.action = na.omit)
fit4 <- lm(similarity ~ differ_year + Learner_ID.f + Leader_ID.f + Year_learner.f, data =  WaterResource , na.action = na.omit)
fit5 <- lm(similarity ~ differ_year + Learner_ID.f + Leader_ID.f + Year_learner.f , data = WaterSaving , na.action = na.omit)
fit6 <- lm(similarity ~ differ_year + Learner_ID.f + Leader_ID.f + Year_learner.f, data = Wetland , na.action = na.omit)
fit7 <- lm(similarity ~ differ_year + Learner_ID.f + Leader_ID.f + Year_learner.f, data = WaterPollution , na.action = na.omit)
fit8 <- lm(similarity ~ differ_year + Leader_ID.f + Year_learner.f, data = CombinedData , na.action = na.omit)

### export TableA4-3
stargazer(fit8, fit1, fit2,fit3,fit4,fit5,fit6,fit7, type = "html",  #we use html output to match our planned R Markdown output
          title = "Linear Regression of Policy Learning - Table1",df = FALSE, notes = "Robust Standard Error in the Parentheses",
          out = "appendix TableA4-3.htm", omit = c("Year_learner", "Learner_ID", "Leader_ID", "Policy"),
          covariate.labels = c("Temporal Distance"), #"Foreign Investment" ,"pm25"
          column.labels = c("Overall",  "Air Pollution", "Green Space", "Noise Pollution", "Water Resource", "Water Saving","Wetland","Water Pollution"),
          add.lines = list(c("Learner Location Dummies","Yes",  "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes"),
                           c("Learner Year Dummies", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes"),
                           c("Source Location Dummies","Yes",  "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes"),
                           c("Policy Fixed effects","Yes",  "N/A", "N/A", "N/A", "N/A", "N/A", "N/A", "N/A")
          ))

### export TableA4-4
fit1 <- lm(similarity ~ innovator_learner_leader + Learner_ID.f + Leader_ID.f + Year_learner.f , data = Air  , na.action = na.omit)
fit2 <- lm(similarity ~ innovator_learner_leader + Learner_ID.f + Leader_ID.f + Year_learner.f , data = Green, na.action = na.omit)
fit3 <- lm(similarity ~ innovator_learner_leader + Learner_ID.f + Leader_ID.f + Year_learner.f , data = Noise, na.action = na.omit)
fit4 <- lm(similarity ~ innovator_learner_leader + Learner_ID.f + Leader_ID.f + Year_learner.f, data =  WaterResource , na.action = na.omit)
fit5 <- lm(similarity ~ innovator_learner_leader + Learner_ID.f + Leader_ID.f + Year_learner.f , data = WaterSaving , na.action = na.omit)
fit6 <- lm(similarity ~ innovator_learner_leader + Learner_ID.f + Leader_ID.f + Year_learner.f, data = Wetland , na.action = na.omit)
fit7 <- lm(similarity ~ innovator_learner_leader + Learner_ID.f + Leader_ID.f + Year_learner.f, data = WaterPollution , na.action = na.omit)
fit8 <- lm(similarity ~ innovator_learner_leader + Leader_ID.f + Year_learner.f, data = CombinedData , na.action = na.omit)

### export TableA4-4
stargazer(fit8, fit1, fit2,fit3,fit4,fit5,fit6,fit7, type = "html",  #we use html output to match our planned R Markdown output
          title = "Linear Regression of Policy Learning - Table1",df = FALSE, notes = "Robust Standard Error in the Parentheses",
          out = "appendix TableA4-4.htm", omit = c("Year_learner", "Learner_ID", "Leader_ID", "Policy"),
          covariate.labels = c("Initial Learning"), #"Foreign Investment" ,"pm25"
          column.labels = c("Overall",  "Air Pollution", "Green Space", "Noise Pollution", "Water Resource", "Water Saving","Wetland","Water Pollution"),
          add.lines = list(c("Learner Location Dummies","Yes",  "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes"),
                           c("Learner Year Dummies", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes"),
                           c("Source Location Dummies","Yes",  "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes"),
                           c("Policy Fixed effects","Yes",  "N/A", "N/A", "N/A", "N/A", "N/A", "N/A", "N/A")
          ))

### export table A6
fit1 <- lm(similarity ~ horizon_learner * Distance + differ_year +  innovator_learner_leader + SimilarSpecialization + city_learner + Self_learer.f + ABSforeigInvestmentBillion_learner_leader + Population_learner_leader + BirthRate_learner_leader + SecondIndustr_learner_leader+ Learner_ID.f + Leader_ID.f + Year_learner.f , data = Air  , na.action = na.omit)
fit2 <- lm(similarity ~ horizon_learner * Distance + differ_year +  innovator_learner_leader + SimilarSpecialization + city_learner + Self_learer.f + ABSforeigInvestmentBillion_learner_leader + Population_learner_leader + BirthRate_learner_leader + SecondIndustr_learner_leader+ Learner_ID.f + Leader_ID.f + Year_learner.f , data = Green, na.action = na.omit)
fit3 <- lm(similarity ~ horizon_learner * Distance + differ_year +  innovator_learner_leader + SimilarSpecialization + city_learner + Self_learer.f + ABSforeigInvestmentBillion_learner_leader + Population_learner_leader + BirthRate_learner_leader + SecondIndustr_learner_leader+ Learner_ID.f + Leader_ID.f + Year_learner.f , data = Noise, na.action = na.omit)
fit4 <- lm(similarity ~ horizon_learner * Distance + differ_year +  innovator_learner_leader + SimilarSpecialization + city_learner + Self_learer.f + ABSforeigInvestmentBillion_learner_leader + Population_learner_leader + BirthRate_learner_leader + SecondIndustr_learner_leader+ Learner_ID.f + Leader_ID.f + Year_learner.f, data =  WaterResource , na.action = na.omit)
fit5 <- lm(similarity ~ horizon_learner * Distance + differ_year +  innovator_learner_leader + SimilarSpecialization + city_learner + Self_learer.f + ABSforeigInvestmentBillion_learner_leader + Population_learner_leader + BirthRate_learner_leader + SecondIndustr_learner_leader+ Learner_ID.f + Leader_ID.f + Year_learner.f , data = WaterSaving , na.action = na.omit)
fit6 <- lm(similarity ~ horizon_learner * Distance + differ_year +  innovator_learner_leader + SimilarSpecialization + city_learner + Self_learer.f + ABSforeigInvestmentBillion_learner_leader + Population_learner_leader + BirthRate_learner_leader + SecondIndustr_learner_leader+ Learner_ID.f + Leader_ID.f + Year_learner.f, data = Wetland , na.action = na.omit)
fit7 <- lm(similarity ~ horizon_learner * Distance + differ_year +  innovator_learner_leader + SimilarSpecialization + city_learner + Self_learer.f + ABSforeigInvestmentBillion_learner_leader + Population_learner_leader + BirthRate_learner_leader + SecondIndustr_learner_leader+ Learner_ID.f + Leader_ID.f + Year_learner.f, data = WaterPollution , na.action = na.omit)
fit8 <- lm(similarity ~ horizon_learner * Distance + differ_year +  innovator_learner_leader + SimilarSpecialization + city_learner + Self_learer.f + ABSforeigInvestmentBillion_learner_leader + Population_learner_leader + BirthRate_learner_leader + SecondIndustr_learner_leader + Policy.f+ Learner_ID.f + Leader_ID.f + Year_learner.f, data = CombinedData , na.action = na.omit)

stargazer(fit8,fit1, fit2,fit3,fit4,fit5,fit6,fit7, type = "html",  #we use html output to match our planned R Markdown output
          title = "Linear Regression of Policy Learning - TableA6",df = FALSE, notes = "Robust Standard Error in the Parentheses",
          out = "appendix A6.htm", omit = c("Year_learner", "Learner_ID", "Leader_ID", "Policy"),
          column.labels = c("Overall",  "Air Pollution", "Green Space", "Noise Pollution", "Water Resource", "Water Saving","Wetland","Water Pollution"),
          add.lines = list(c("Learner Location Dummies","Yes",  "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes"),
                           c("Learner Year Dummies", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes"),
                           c("Source Location Dummies","Yes",  "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes"),
                           c("Policy Fixed effects","Yes",  "N/A", "N/A", "N/A", "N/A", "N/A", "N/A", "N/A")
          ))

### export table A7
fit1 <- lm(similarity ~ horizon_learner + Distance + differ_year *  innovator_learner_leader + SimilarSpecialization + city_learner + Self_learer.f + ABSforeigInvestmentBillion_learner_leader + Population_learner_leader + BirthRate_learner_leader + SecondIndustr_learner_leader+ Learner_ID.f + Leader_ID.f + Year_learner.f , data = Air  , na.action = na.omit)
fit2 <- lm(similarity ~ horizon_learner + Distance + differ_year *  innovator_learner_leader + SimilarSpecialization + city_learner + Self_learer.f + ABSforeigInvestmentBillion_learner_leader + Population_learner_leader + BirthRate_learner_leader + SecondIndustr_learner_leader+ Learner_ID.f + Leader_ID.f + Year_learner.f , data = Green, na.action = na.omit)
fit3 <- lm(similarity ~ horizon_learner + Distance + differ_year *  innovator_learner_leader + SimilarSpecialization + city_learner + Self_learer.f + ABSforeigInvestmentBillion_learner_leader + Population_learner_leader + BirthRate_learner_leader + SecondIndustr_learner_leader+ Learner_ID.f + Leader_ID.f + Year_learner.f , data = Noise, na.action = na.omit)
fit4 <- lm(similarity ~ horizon_learner + Distance + differ_year *  innovator_learner_leader + SimilarSpecialization + city_learner + Self_learer.f + ABSforeigInvestmentBillion_learner_leader + Population_learner_leader + BirthRate_learner_leader + SecondIndustr_learner_leader+ Learner_ID.f + Leader_ID.f + Year_learner.f, data =  WaterResource , na.action = na.omit)
fit5 <- lm(similarity ~ horizon_learner + Distance + differ_year *  innovator_learner_leader + SimilarSpecialization + city_learner + Self_learer.f + ABSforeigInvestmentBillion_learner_leader + Population_learner_leader + BirthRate_learner_leader + SecondIndustr_learner_leader+ Learner_ID.f + Leader_ID.f + Year_learner.f , data = WaterSaving , na.action = na.omit)
fit6 <- lm(similarity ~ horizon_learner + Distance + differ_year *  innovator_learner_leader + SimilarSpecialization + city_learner + Self_learer.f + ABSforeigInvestmentBillion_learner_leader + Population_learner_leader + BirthRate_learner_leader + SecondIndustr_learner_leader+ Learner_ID.f + Leader_ID.f + Year_learner.f, data = Wetland , na.action = na.omit)
fit7 <- lm(similarity ~ horizon_learner + Distance + differ_year *  innovator_learner_leader + SimilarSpecialization + city_learner + Self_learer.f + ABSforeigInvestmentBillion_learner_leader + Population_learner_leader + BirthRate_learner_leader + SecondIndustr_learner_leader+ Learner_ID.f + Leader_ID.f + Year_learner.f, data = WaterPollution , na.action = na.omit)
fit8 <- lm(similarity ~ horizon_learner + Distance + differ_year *  innovator_learner_leader + SimilarSpecialization + city_learner + Self_learer.f + ABSforeigInvestmentBillion_learner_leader + Population_learner_leader + BirthRate_learner_leader + SecondIndustr_learner_leader + Policy.f+ Learner_ID.f + Leader_ID.f + Year_learner.f, data = CombinedData , na.action = na.omit)

stargazer(fit8,fit1, fit2,fit3,fit4,fit5,fit6,fit7, type = "html",  #we use html output to match our planned R Markdown output
          title = "Linear Regression of Policy Learning - TableA7",df = FALSE, notes = "Robust Standard Error in the Parentheses",
          out = "appendix A7.htm", omit = c("Year_learner", "Learner_ID", "Leader_ID", "Policy"),
          column.labels = c("Overall",  "Air Pollution", "Green Space", "Noise Pollution", "Water Resource", "Water Saving","Wetland","Water Pollution"),
          add.lines = list(c("Learner Location Dummies","Yes",  "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes"),
                           c("Learner Year Dummies", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes"),
                           c("Source Location Dummies","Yes",  "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes"),
                           c("Policy Fixed effects","Yes",  "N/A", "N/A", "N/A", "N/A", "N/A", "N/A", "N/A")
          ))

### export table A8
fit1 <- lm(similarity ~ horizon_learner +  differ_year + Distance * innovator_learner_leader + SimilarSpecialization + city_learner + Self_learer.f + ABSforeigInvestmentBillion_learner_leader + Population_learner_leader + BirthRate_learner_leader + SecondIndustr_learner_leader+ Learner_ID.f + Leader_ID.f + Year_learner.f , data = Air  , na.action = na.omit)
fit2 <- lm(similarity ~ horizon_learner +  differ_year + Distance * innovator_learner_leader + SimilarSpecialization + city_learner + Self_learer.f + ABSforeigInvestmentBillion_learner_leader + Population_learner_leader + BirthRate_learner_leader + SecondIndustr_learner_leader+ Learner_ID.f + Leader_ID.f + Year_learner.f , data = Green, na.action = na.omit)
fit3 <- lm(similarity ~ horizon_learner +  differ_year + Distance * innovator_learner_leader + SimilarSpecialization + city_learner + Self_learer.f + ABSforeigInvestmentBillion_learner_leader + Population_learner_leader + BirthRate_learner_leader + SecondIndustr_learner_leader+ Learner_ID.f + Leader_ID.f + Year_learner.f , data = Noise, na.action = na.omit)
fit4 <- lm(similarity ~ horizon_learner +  differ_year + Distance * innovator_learner_leader + SimilarSpecialization + city_learner + Self_learer.f + ABSforeigInvestmentBillion_learner_leader + Population_learner_leader + BirthRate_learner_leader + SecondIndustr_learner_leader+ Learner_ID.f + Leader_ID.f + Year_learner.f, data =  WaterResource , na.action = na.omit)
fit5 <- lm(similarity ~ horizon_learner +  differ_year + Distance * innovator_learner_leader + SimilarSpecialization + city_learner + Self_learer.f + ABSforeigInvestmentBillion_learner_leader + Population_learner_leader + BirthRate_learner_leader + SecondIndustr_learner_leader+ Learner_ID.f + Leader_ID.f + Year_learner.f , data = WaterSaving , na.action = na.omit)
fit6 <- lm(similarity ~ horizon_learner +  differ_year + Distance * innovator_learner_leader + SimilarSpecialization + city_learner + Self_learer.f + ABSforeigInvestmentBillion_learner_leader + Population_learner_leader + BirthRate_learner_leader + SecondIndustr_learner_leader+ Learner_ID.f + Leader_ID.f + Year_learner.f, data = Wetland , na.action = na.omit)
fit7 <- lm(similarity ~ horizon_learner +  differ_year + Distance * innovator_learner_leader + SimilarSpecialization + city_learner + Self_learer.f + ABSforeigInvestmentBillion_learner_leader + Population_learner_leader + BirthRate_learner_leader + SecondIndustr_learner_leader+ Learner_ID.f + Leader_ID.f + Year_learner.f, data = WaterPollution , na.action = na.omit)
fit8 <- lm(similarity ~ horizon_learner +  differ_year + Distance * innovator_learner_leader + SimilarSpecialization + city_learner + Self_learer.f + ABSforeigInvestmentBillion_learner_leader + Population_learner_leader + BirthRate_learner_leader + SecondIndustr_learner_leader + Policy.f+ Learner_ID.f + Leader_ID.f + Year_learner.f, data = CombinedData , na.action = na.omit)

stargazer(fit8,fit1, fit2,fit3,fit4,fit5,fit6,fit7, type = "html",  #we use html output to match our planned R Markdown output
          title = "Linear Regression of Policy Learning - TableA8",df = FALSE, notes = "Robust Standard Error in the Parentheses",
          out = "appendix A8.htm", omit = c("Year_learner", "Learner_ID", "Leader_ID", "Policy"),
          column.labels = c("Overall",  "Air Pollution", "Green Space", "Noise Pollution", "Water Resource", "Water Saving","Wetland","Water Pollution"),
          add.lines = list(c("Learner Location Dummies","Yes",  "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes"),
                           c("Learner Year Dummies", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes"),
                           c("Source Location Dummies","Yes",  "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes"),
                           c("Policy Fixed effects","Yes",  "N/A", "N/A", "N/A", "N/A", "N/A", "N/A", "N/A")
          ))

### export table A9
fit1 <- lm(similarity ~ horizon_learner +  differ_year + innovator_learner_leader + Distance * SimilarSpecialization  + city_learner + Self_learer.f + ABSforeigInvestmentBillion_learner_leader + Population_learner_leader + BirthRate_learner_leader + SecondIndustr_learner_leader+ Learner_ID.f + Leader_ID.f + Year_learner.f , data = Air  , na.action = na.omit)
fit2 <- lm(similarity ~ horizon_learner +  differ_year + innovator_learner_leader + Distance * SimilarSpecialization  + city_learner + Self_learer.f + ABSforeigInvestmentBillion_learner_leader + Population_learner_leader + BirthRate_learner_leader + SecondIndustr_learner_leader+ Learner_ID.f + Leader_ID.f + Year_learner.f , data = Green, na.action = na.omit)
fit3 <- lm(similarity ~ horizon_learner +  differ_year + innovator_learner_leader + Distance * SimilarSpecialization  + city_learner + Self_learer.f + ABSforeigInvestmentBillion_learner_leader + Population_learner_leader + BirthRate_learner_leader + SecondIndustr_learner_leader+ Learner_ID.f + Leader_ID.f + Year_learner.f , data = Noise, na.action = na.omit)
fit4 <- lm(similarity ~ horizon_learner +  differ_year + innovator_learner_leader + Distance * SimilarSpecialization  + city_learner + Self_learer.f + ABSforeigInvestmentBillion_learner_leader + Population_learner_leader + BirthRate_learner_leader + SecondIndustr_learner_leader+ Learner_ID.f + Leader_ID.f + Year_learner.f, data =  WaterResource , na.action = na.omit)
fit5 <- lm(similarity ~ horizon_learner +  differ_year + innovator_learner_leader + Distance * SimilarSpecialization  + city_learner + Self_learer.f + ABSforeigInvestmentBillion_learner_leader + Population_learner_leader + BirthRate_learner_leader + SecondIndustr_learner_leader+ Learner_ID.f + Leader_ID.f + Year_learner.f , data = WaterSaving , na.action = na.omit)
fit6 <- lm(similarity ~ horizon_learner +  differ_year + innovator_learner_leader + Distance * SimilarSpecialization  + city_learner + Self_learer.f + ABSforeigInvestmentBillion_learner_leader + Population_learner_leader + BirthRate_learner_leader + SecondIndustr_learner_leader+ Learner_ID.f + Leader_ID.f + Year_learner.f, data = Wetland , na.action = na.omit)
fit7 <- lm(similarity ~ horizon_learner +  differ_year + innovator_learner_leader + Distance * SimilarSpecialization  + city_learner + Self_learer.f + ABSforeigInvestmentBillion_learner_leader + Population_learner_leader + BirthRate_learner_leader + SecondIndustr_learner_leader+ Learner_ID.f + Leader_ID.f + Year_learner.f, data = WaterPollution , na.action = na.omit)
fit8 <- lm(similarity ~ horizon_learner +  differ_year + innovator_learner_leader + Distance * SimilarSpecialization  + city_learner + Self_learer.f + ABSforeigInvestmentBillion_learner_leader + Population_learner_leader + BirthRate_learner_leader + SecondIndustr_learner_leader + Policy.f+ Learner_ID.f + Leader_ID.f + Year_learner.f, data = CombinedData , na.action = na.omit)

stargazer(fit8,fit1, fit2,fit3,fit4,fit5,fit6,fit7, type = "html",  #we use html output to match our planned R Markdown output
          title = "Linear Regression of Policy Learning - TableA9",df = FALSE, notes = "Robust Standard Error in the Parentheses",
          out = "appendix A9.htm", omit = c("Year_learner", "Learner_ID", "Leader_ID", "Policy"),
          column.labels = c("Overall",  "Air Pollution", "Green Space", "Noise Pollution", "Water Resource", "Water Saving","Wetland","Water Pollution"),
          add.lines = list(c("Learner Location Dummies","Yes",  "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes"),
                           c("Learner Year Dummies", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes"),
                           c("Source Location Dummies","Yes",  "Yes", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes"),
                           c("Policy Fixed effects","Yes",  "N/A", "N/A", "N/A", "N/A", "N/A", "N/A", "N/A")
          ))
